package ar.fi.uba.fallas2.business;

import java.sql.PreparedStatement;

import ar.fi.uba.fallas2.data.DataException;
import ar.fi.uba.fallas2.data.Database;

public class Question extends BusinessObject {
	private String question;
	private static final String insertStatementString = "INSERT INTO question(value) VALUES (?)";
	private static final String updateStatementString = "UPDATE question SET value = ? WHERE id_question = ?";
	private static final String deleteStatementString = "DELETE FROM question WHERE id_question = ?";
	
	public Question() {
		super("question");
	}
	
	public Question(int id) {
		super("question", id);
	}
	
	public boolean equals(Object o) {
		Question other = (Question) o;
		return this.getId() == other.getId();
	}
	
	public int hashCode() {
		return 0;
	}
	
	protected void insert() throws DataException {
		PreparedStatement insertStatement = null;
		try {
			insertStatement = Database.getInstance().prepareStatement(insertStatementString);
			insertStatement.setString(1, this.getQuestion());
			insertStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	protected void update() throws DataException {
		PreparedStatement updateStatement = null;
		try {
			updateStatement = Database.getInstance().prepareStatement(updateStatementString);
			updateStatement.setString(1, this.getQuestion());
			updateStatement.setInt(2, this.getId());
			updateStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public void delete() throws DataException {
		PreparedStatement updateStatement = null;
		try {
			updateStatement = Database.getInstance().prepareStatement(deleteStatementString);
			updateStatement.setInt(1, this.getId());
			updateStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getQuestion() {
		return question;
	}
}
